package cn.kelvin.oocl.dayone;

import java.util.Arrays;

public class Sort {
	public static void main(String[] args) {
		int[] n = {5,2,7,4,1,12,8,3,10,13,9,11,6};
//		seleSort(n);
//		bubbleSort(n);
		bubbleSort1(n);
//		insertSort(n);
		System.out.println(Arrays.toString(n));
		System.out.println(binarySearch(6, n));
	}
	
	public static int binarySearch(int value,int[] sortedAs){
		int low = 0;
		int high = sortedAs.length-1;
		int middle=0;
		while(low<=high){
			middle = (low+high)/2;
			if(value==sortedAs[middle]){
				return middle;
			}else if(value<sortedAs[middle]){
//				high = --middle;
				high = middle-1;
			}else if(value>sortedAs[middle]){
//				low = ++middle;
				low = middle+1;
			}
		}
		return middle;
	}

	private static void insertSort(int[] n) {
		int min,k;
		for(int i=1;i<n.length;i++){
			min = n[i];
			k = i;
			int j = i;
			while(j>0 && n[j]>min){
				k=j;
				j--;
			}
			if(i!=k){
				n[i]=n[k];
				n[k]=min;
			}
		}
	}
	
	private static void bubbleSort1(int[] n) {
		int temp;
		for(int i=n.length-1;i>0;i--){
			for(int j=0;j<i;j++){
				if(n[j+1]<n[j]){
					temp = n[j];
					n[j]=n[j+1];
					n[j+1]=temp;
				}
			}
		}
	}

	private static void bubbleSort(int[] n) {
		int temp;
		for(int i=0;i<n.length-1;i++){
			for(int j=n.length-1;j>i;j--){
				if(n[j]<n[j-1]){
					temp = n[j];
					n[j]=n[j-1];
					n[j-1]=temp;
				}
			}
		}
	}

	private static void seleSort(int[] n) {
		int min,k;
		for(int i=0;i<n.length-1;i++){
			min = n[i];
			k=i;
			for(int j=i+1;j<n.length;j++){
				if(n[j]<min){
					min = n[j];
					k=j;
				}
			}
			if(k!=i){
				n[k] = n[i];
				n[i] = min;
			}
		}
	}
}
